package com.example.views;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

	String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy",
			"Lyndon B. Johnson", "Richard Nixon", "Gerald Ford",
			"Jimmy Carter", "Ronald Reagan", "George H. W. Bush",
			"Bill Clinton", "George W. Bush", "Barack Obama" };

	TimePicker timePicker;

	DatePicker datePicker;

	int hour, minute;
	int yr, month, day;

	static final int TIME_DIALOG_ID = 0;
	static final int DATE_DIALOG_ID = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Botton On Click Listener
		Button button_save = (Button) findViewById(R.id.btnSave);
		button_save.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DisplayToast("You have clicked the Save button");

			}
		});

		Button button_open = (Button) findViewById(R.id.btnOpen);
		button_open.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DisplayToast("You have clicked the Open button");
				Intent intent = new Intent("com.example.views.BasicView2");
				startActivity(intent);
			}
		});

		// AutoCompleteTextView Setup
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, presidents);
		AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
		textView.setThreshold(3);
		textView.setAdapter(adapter);

		// ImageButton On Click Listener
		ImageButton imageButton = (ImageButton) findViewById(R.id.btnImg1);
		imageButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DisplayToast("You have clicked the Image button");

			}
		});

		// TimePicker Set Up
		timePicker = (TimePicker) findViewById(R.id.timePicker);
		timePicker.setIs24HourView(true);
		
		// To set Time in a Dialog Box
		// showDialog(TIME_DIALOG_ID);
		
		// Date Picker Set Up
		datePicker = (DatePicker) findViewById(R.id.datePicker);

		// ---get the current date---
		Calendar today = Calendar.getInstance();
		yr = today.get(Calendar.YEAR);
		month = today.get(Calendar.MONTH);
		day = today.get(Calendar.DAY_OF_MONTH);
		showDialog(DATE_DIALOG_ID);

		// CheckBox On Click Listener
		CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave);
		checkBox.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (((CheckBox) v).isChecked()) {
					DisplayToast("CheckBox is checked");
				} else {
					DisplayToast("CheckBox is unchecked");
				}

			}
		});

		// RadioGroup On Checked Change Listener
		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1);
				DisplayToast("" + checkedId);
				if (rb1.isChecked()) {
					DisplayToast("Option 1 checked!");
				} else {
					DisplayToast("Option 2 checked!");
				}
			}
		});

		// ToggleButton On Click Listener
		ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle1);
		toggleButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (((ToggleButton) v).isChecked()) {
					DisplayToast("Toggle button is On");
				} else {
					DisplayToast("Toggle button is Off");
				}
			}
		});
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener, hour, minute,
					false);
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, yr, month, day);
		}
		return null;
	}

	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour) {
			hour = hourOfDay;
			minute = minuteOfHour;
			SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
			Date date = new Date(0, 0, 0, hour, minute);
			String strDate = timeFormat.format(date);
			Toast.makeText(getBaseContext(), "You have selected " + strDate,
					Toast.LENGTH_SHORT).show();
		}
	};

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			yr = year;
			month = monthOfYear;
			day = dayOfMonth;
			Toast.makeText(
					getBaseContext(),
					"You have selected : " + (month + 1) + "/" + day + "/"
							+ year, Toast.LENGTH_SHORT).show();
		}
	};

	public void onClick(View view) {
		// For Displaying set Date & time
		DisplayToast("Date selected:" + (datePicker.getMonth() + 1) + "/"
				+ datePicker.getDayOfMonth() + "/" + datePicker.getYear()
				+ "\n" + "Time selected:" + timePicker.getCurrentHour() + ":"
				+ timePicker.getCurrentMinute());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void DisplayToast(String msg) {
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
	}
}
